		
	/* 
		our library helper to manipulate GMap
		please note that all of the map function here need a map tobe initialized first
	*/	
	

	// ===    Navigation Functions 
	/*
	 * set the map center and zoom level, used for navigating between maps
	 */
	function setCenterMap(lat,lang, zoom) {
		map.setCenter(new GLatLng(lat, lang), zoom);
	}
	
	/*
	 * Resetting navigation. Setting the view back to indonesia
	 */
	function resetNav() {
		map.setCenter(new GLatLng(-2.1088986592431254, 117.158203125), 5);		
	}
	
	
	// ===  Map Behaviour function
	/*
	 * Showing polygon that has been saved into the database
	 */
    function showOtherPolygon() {
    	if(other_polygon.length > 0) {
            for (var i=0; i<other_polygon.length; i++) {
    			other_polygon[i].show();
    		}            
            return true;
        }
    	
    	return false;
    	
    }

    /*
     * Hiding the existing polygon
     */
    function hideOtherPolygon() {
        for (var i=0; i<other_polygon.length; i++) {
			other_polygon[i].hide();
		}
    }

    /*
     * Showing edited markers
     */
    function show() {
      for (var i=0; i<gmarkers.length; i++) {
          gmarkers[i].show();
      }
    }

    /*
     * Hides edited markers
     */
    function hide() {
      for (var i=0; i<gmarkers.length; i++) {
          gmarkers[i].hide();
      }
      // == clear the checkbox ==
      //document.getElementById(category+"box").checked = false;
      // == close the info window, in case its open on a marker that we just hid
      //map.closeInfoWindow();
    }      
    
    /*
     * Removing edited markers, clearing the array, and start again from the beginning of marker collection
     * also removes the polygon we retrieve from database from the current view 
     */
    function clearMarkers() {
    	// remove all overlay, including the other layers
    	// should update to clear only the polygon we draw
    	map.clearOverlays();
        gmarkers = [];
        other_polygon = [];
    }
     
    function showPolygon(areaMarkerData) {
        for (var i=0; i<areaMarkerData.length; i++) {
        	buildAPolygon(areaMarkerData[i]);
        }

    }

    
    // === Map Maker Function
    
    /*
     * deleting last point that has been clicked / undo
     */    		
    function deleteLastPoint(){    	  
    	  map.removeOverlay(gmarkers.pop());
    } 
    
    /*
     * Drawing the polygon by connecting all of the marker that we have selected
     */
    function createPolygon() {
        var polyPoints = [];
        for (var i=0; i<gmarkers.length; i++) {
			polyPoints[i] = gmarkers[i].getLatLng();
        }  
        // closing the polygon
        polyPoints[polyPoints.length] = gmarkers[0].getLatLng();
    	
        var polygon = new GPolygon(polyPoints, "#f33f00", 2, 1, "#ff0000", 0.2);
    	map.addOverlay(polygon);
        document.mapForm.hid.value = createMarkerData(gmarkers);
	}
        
    /*
     * Building a polygon from the database, passing as a JSON array from the underlaying layer
     * this method will parse the data and return it to its lat-lng format
     */
    function buildAPolygon(areaJSON) {
    	var markerData = areaJSON.markers;
    	var polyData = [];
        for (var i=0; i<markerData.length; i=i+2) {
        	var latLng= new GLatLng(markerData[i], markerData[i+1]);
        	polyData.push(latLng);
        }
        polyData.push(new GLatLng(markerData[0], markerData[1]));
    	var polygon = new GPolygon(polyData, areaJSON.defaultAreaColor, 2, 1, areaJSON.defaultLineColor, 0.2);
    	map.addOverlay(polygon);
    	other_polygon.push(polygon);
    }
    
    /*
     * Turning marker data from gmarker array into string to be passed and save to the underlaying layer (tapestry page)
     * through a hidden variable
     */
    function createMarkerData(gmarkers) {
    	var markerData;
        for (var i=0; i<gmarkers.length; i++) {
			markerData += gmarkers[i].getLatLng() + ":";
        }  
        
        return markerData;
    }
    
    // ==== Polygon function 
    /*
     * Getting the center of the polygon that is currently selected
     * iterate over the available polygon, check if the point is inside the polygon. Get the boundary of this polygon
     * then get the center of the polygon. (ini lebih ke how, bukan what nanti perlu di refine javadocs yang ada)
     */
    function getPolygonCenter(latlng) {
        for (var i=other_polygon.length-1; i > 0; i--) {
        	var bounds = other_polygon[i].getBounds();
        	if (bounds.containsLatLng(latlng)) {
        		//return bounds.getCenter();
        		return bounds;
        	}
        }
        return "out of polygon bound";
    }
    